package vip.finance.makemoney.config.exception;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.RecoverableDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import vip.finance.makemoney.common.RestEnum;
import vip.finance.makemoney.common.WebJson;

/**
 * Created by yangbin on 2023/1/19
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 用于处理异常的
     *
     * @return
     */
    @ExceptionHandler({Exception.class})
    @ResponseBody
    public WebJson<Object> exception(Exception e, HttpServletRequest request) {
        log.error("==GlobalExceptionHandler-->exception:", e);
        //自定义异常
        if (e instanceof BusinessException) {
            BusinessException e1 = (BusinessException) e;
            return WebJson.data(((BusinessException) e).getStatus(), ((BusinessException) e).getMsg());
        }
        if (e instanceof DeadlockLoserDataAccessException) {
            return WebJson.data(RestEnum.SERVICE_ERROR, "您操作太频繁了，请稍后操作");
        }
        if (e instanceof HttpRequestMethodNotSupportedException) {
            return WebJson.data(RestEnum.SERVICE_ERROR, "请求方式错误");
        }
        if (e instanceof DuplicateKeyException) {
            return WebJson.data(RestEnum.SERVICE_ERROR, "重复写入错误");
        }
        if (e instanceof RecoverableDataAccessException) {
            return WebJson.data(RestEnum.SERVICE_ERROR, "数据读取失败：" + e.getMessage());
        }
        if (e instanceof UncategorizedSQLException) {
            return WebJson.data(RestEnum.SERVICE_ERROR, "数据格式不对，请检查数据");
        }
        if (e instanceof DataIntegrityViolationException) {
            return WebJson.data(RestEnum.SERVICE_ERROR, "数据异常，空值或过长，请检查数据");
        }
        if (e instanceof CannotGetJdbcConnectionException) {
            return WebJson.data(RestEnum.SERVICE_ERROR, "数据库连接异常");
        }
        if (e instanceof NoHandlerFoundException) {
            return WebJson.data(HttpStatus.NOT_FOUND.value(), e.getMessage());
        }
        if (e instanceof MissingServletRequestParameterException) {
            return WebJson.data(RestEnum.PARAMETER_INVALID);
        }
        if (e instanceof ServletException) {
            e.printStackTrace();
            // return RestJson.error(response.getStatus(), HttpStatus.valueOf(response.getStatus()).getReasonPhrase());
        } else {
            e.printStackTrace();
        }
        return WebJson.data(RestEnum.SERVICE_ERROR, "服务器开小车了，请联系运维小哥哥");
    }
}
